import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== declare
const { $Lazy, $fn } = window
// ===================================================================== Lazy
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component

class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			// { label: '科室',			name: 'project_pid', 			half:true,  	type: 'select',		idStr: 'uuid',	required:true },
			// { label: '岗位',			name: 'project_id', 			half:true, 		type: 'select',	required:true },
			{ label: '科室',				name: 'project_pid', 	half:true,  type: 'select',		idStr: 'uuid',	required:true },
			{ label: '岗位',				name: 'project_id', 	half:true, 	type: 'select',		required:true },
			// { label: '实验号模板',	name: 'tag_id', 				half:true, 		type: 'select',		onValue:(row)=>{console.log(row)} },
			// { label: '周期',			name: 'lab_batch',  			half:true },
			{ label: '开始时间',		name: 'start_at', 				half:true,		type: 'date-time',	required:true,	showTimeStart:true },
			{ label: '结束时间',		name: 'end_at', 				half:true,		type: 'date-time',	required:true,	showTimeEnd:true },
			// { label: '周期时间',		names: ['start_at','end_at'], 	full:true,		type:'date-range', name: 'jz-time'  },
			{ label: '开始实验号',	name: 'start_experiment_num',  	half:true,		onValue:({value})=>{this.start_code = value} },
			{ label: '结束实验号',	name: 'end_experiment_num',  	half:true,		onValue:({value})=>{this.end_code = value},	
				btn:{ label:'生成', 	 onClick:value=>{this.generateCode()}}
			},
			{ label: '实验号',		name: 'experiment_nums',  	full:true,		type:'textarea',	required:true, },
		],
		api: '',
		title: ''
	}
	
	start_code= ''
	end_code= ''
	exp_nums= []
	submit= {}
	
	open = ({uuid, api, title}) => {
		this.submit.experiment_nums = ''
		this.exp_nums= []
		this.getModalSelect()
		this.setState({api, title})
		this.modal.open({ formRef: this.formRef })
		this.submit.uuid = uuid
		this.submit.action = 'experiment_nums'
	}
	
	
	// 获取弹窗下拉
	getModalSelect = () => {
		const { formData } = this.state
			
		$cache.then( async f => {
			// 岗位
			$fn.setRow(formData, 'project_id',  await f.getCache({ api: 'gang_wei' }))
			// 科室
			$fn.setRow(formData, 'project_pid', await f.getCache({ api: 'ke_shi' }))
			// 实验号模板
			// $fn.setRow(formData, 'tag_id', await f.getCache({ api: 'num_template' }))
			
			this.setState({ formData })			
		})
	}
	
	// 生成实验号
	generateCode = () => {
		const {start_code, end_code } = this
		const arr =[]
		const start_codeNum = parseInt(start_code.replace(/[^0-9]/ig, '')) //开始实验号数字部分
		const end_codeNum = parseInt(end_code.replace(/[^0-9]/ig, '')) //结束实验号数字部分
		if (end_codeNum < start_codeNum) return message.then(f => f.warning('结束实验号不能小于开始实验号'))
		const noNum = start_code.replace(/[^0-9]/ig, '').length //实验号数字长度
		const strNum = start_code.length //实验号长度
		const str = start_code.slice(0, strNum - noNum) //实验号前缀
		for(var i = start_codeNum; i <= end_codeNum; i++){
			arr.push(i)
		} // 生成实验号数字部分
		this.exp_nums = Array.from(new Set([...this.exp_nums, ...arr])) //实验号数字数组
		const newExp_nums = this.exp_nums.map(v => str + v.toString().padStart(noNum, '0')) //组装实验号
		this.formRef.setValue({experiment_nums: newExp_nums.join('\r')}) //渲染实验号
	}
	
	// 解析条码
	getSpec = () => {
		let {experiment_nums} = this.formRef.getValue(['experiment_nums'])
		experiment_nums = experiment_nums.match(/[a-zA-Z]+\d+/g)
		this.submit.experiment_nums  = Array.from(new Set(experiment_nums)).join(',')
	}
	
	// 获取岗位
	// getJobs = value => {
	// 	window.$http.submit(null,'project-team/jobselect', { param: { uuid: value }, isPull:true }).then(data=>{
	// 		const { formData } = this.state
	// 		if($fn.hasArray(data)){
	// 			$cache.then( async f => {
	// 				// 岗位
	// 				$fn.setRow(formData, 'project_id', data)
	// 				$fn.getRow(formData,'project_id').p = '请选择岗位'
	// 				this.setState({ formData })
	// 			})
	// 		}
	// 	})
	// }
	
	render(){
		const { api, title } = this.state
		return FormModal.call(this,{ title, labelWidth:'s6', width:700, clear:true, api, enter:false, onOk: this.getSpec })
	}
}

export default Index